//
//  RootViewController.m
//  project
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "RootViewController.h"
#import "ZZConfig.h"
#import "HomePageViewController.h"
#import "ServiceViewController.h"
#import "ProductViewController.h"
#import "ListViewController.h"
#import "ProgressViewController.h"
#import "UIView+Genie.h"
#import <QuartzCore/QuartzCore.h>
#import "ShoppingCartView.h"
#import "AppDelegate.h"

@interface RootViewController ()
@property (nonatomic, strong) UIImageView               *ZZNav;
@property (nonatomic, strong) UIView                    *tabbarView;
@property (nonatomic, strong) UIButton                  *zzBtn;
@property (nonatomic, strong) UIButton                  *lastBtn;
@property (nonatomic, strong) UIView                    *goShopView;
@property (nonatomic, strong) UIView                    *lastView;
@property (nonatomic, strong) HomePageViewController    *homePageVC;
@property (nonatomic, strong) ServiceViewController     *serviceVC;
@property (nonatomic, strong) ListViewController        *listVC;
@property (nonatomic, strong) ProgressViewController    *progressVC;
@property (nonatomic, strong) ProductViewController     *productVC;
@property (nonatomic, assign) NSInteger                 lastTag;
@property (nonatomic, strong) UIButton                  *back;

/**购物车一类里面的东西*/
@property (strong, nonatomic) ShoppingCartView                    *draggedView;//购物车的View

//@property (strong, nonatomic)
@property (nonatomic)BOOL  shoppingCartIsOpened;
/**购物车红点图片*/
@property (nonatomic, strong) UIImageView       *numberImage;
/**购物车红点数字*/
@property (nonatomic, strong) UILabel           *numberLabel;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"背景"]];
    [self creatTabBar];
    [self creatZNav];
    [self draggedView];
    [self back];
    [self nameLabel];
    [self passwordLabel];
}

- (void)creatTabBar{
    _homePageVC = [[HomePageViewController alloc] init];
    _serviceVC = [[ServiceViewController alloc] init];
    _productVC = [[ProductViewController alloc] init];
    _listVC = [[ListViewController alloc] init];
    _progressVC = [[ProgressViewController alloc] init];
    [self addChildViewController:_serviceVC];
    [self addChildViewController:_productVC];
    [self addChildViewController:_listVC];
    [self addChildViewController:_progressVC];
    [self addChildViewController:_homePageVC];
    
    /** numberlabel 加1 */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AddOne) name:@"numberLabelPlusOne" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minusOne) name:@"minusOne" object:nil];
    
    //tabBar标签栏的高度为49
    _tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 80 , 80, SCREEN_HEIGHT -80)];
    _tabbarView.userInteractionEnabled = YES;
    _tabbarView.backgroundColor = barGrayColor;
    [self.view addSubview:_tabbarView];

    NSArray *nomalImages = @[@"首页_03",@"出国服务_05",@"理财产品_07",@"选购清单_09",@"服务进度_11"];
    NSArray *selectImages = @[@"首页_03_h",@"出国服务_05_h",@"理财产品_07_h",@"选购清单_09_h",@"服务进度_11_h"];
    for (NSInteger i = 0; i < 5; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0 + 95 * i, 80, 95);
        btn.tag = 200 +i;
        if (i == 0) {
            btn.selected = YES;
            _lastBtn = btn;
            _lastTag = 200;
        }
        [btn setImage:[UIImage imageNamed:nomalImages[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectImages[i]] forState:UIControlStateSelected];
        btn.adjustsImageWhenHighlighted = NO;
        
        [btn addTarget:self action:@selector(btnTap:) forControlEvents:(UIControlEventTouchUpInside)];
        [_tabbarView addSubview:btn];
    }
    
    UIButton *trashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    trashBtn.frame = CGRectMake(10, 610, 60, 50);
    [trashBtn addTarget: self action:@selector(trashBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];

    [trashBtn setImage: [UIImage imageNamed:@"购物车_01"] forState:UIControlStateNormal];
    [_tabbarView addSubview:trashBtn];
    [trashBtn addSubview:self.numberImage];
    [trashBtn addSubview:self.numberLabel];
}

- (void)creatZNav{
    _ZZNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    _ZZNav.image = [UIImage imageNamed:@"眉头_01"];
    [self.view addSubview:_ZZNav];
    _ZZNav.userInteractionEnabled = YES;
    UIImageView *headImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 55, 55)];
    headImage.image = [UIImage imageNamed:@"头像_11"];
    [_ZZNav addSubview:headImage];
}

#pragma mark - 购物车加 1 减 1 的通知
- (void)AddOne
{
    NSString *string = [NSString stringWithFormat:@"%ld",[self.numberLabel.text integerValue] + 1];
    self.numberLabel.text = string;
}

- (void)minusOne
{
    NSString *string = [NSString stringWithFormat:@"%ld",[self.numberLabel.text integerValue] - 1];
    self.numberLabel.text = string;
}

#pragma mark   ---  点击事件
- (void)btnTap:(UIButton *)sender{
    UIButton *btn = [self.view viewWithTag:sender.tag];
    //先设置每一个的frame
    
    if (_lastBtn != nil && _lastBtn != btn) {
        _lastBtn.selected = NO;
        _lastBtn.userInteractionEnabled = YES;
    }
    if (btn != _lastBtn) {
        btn.selected = YES;
        btn.userInteractionEnabled = NO;
        
        
        //view在屏幕上面
        CGRect topRect = CGRectMake(80, -SCREEN_HEIGHT+160,  SCREEN_WIDTH-80, SCREEN_HEIGHT -80);
        //view在屏幕中间
        CGRect  middleRect = CGRectMake(80, 80, SCREEN_WIDTH-80, SCREEN_HEIGHT -80);
        //view在屏幕下面
        CGRect  downRect = CGRectMake(80, SCREEN_HEIGHT, SCREEN_WIDTH-80, SCREEN_HEIGHT -80);
        //动画逻辑部分
        UIView *currentView = [self getCurrentView:sender.tag];
        UIView *lastView = [self getCurrentView:_lastTag];
        //如果购物车界面出现
        if (_shoppingCartIsOpened == YES) {
            CGRect endRect = CGRectMake(45, 720, 1, 1);
            [_draggedView genieInTransitionWithDuration:1
                                        destinationRect:endRect
                                        destinationEdge:BCRectEdgeRight
                                             completion:^{
                                                 
                                                 _shoppingCartIsOpened = NO;
                                             }];
            _draggedView.hidden = YES;
            currentView.frame = middleRect;
            lastView.frame = downRect;
        }else {
            if (sender.tag > _lastTag) {
                currentView.frame = downRect;
                [UIView animateWithDuration:0.5 animations:^{
                    //上滑
                    currentView.frame = middleRect;
                    lastView.frame = topRect;
                }];
                
            }
            else{
                currentView.frame = topRect;
                [UIView animateWithDuration:0.5 animations:^{
                    //下滑
                    currentView.frame = middleRect;
                    lastView.frame = downRect;
                }];
                
            }
            
        }
        
        _lastBtn = btn;
    }
    
    _lastTag = sender.tag;
}



-(void)trashBtnTap:(UIButton *)sender{
    
    if (_shoppingCartIsOpened == NO) {
        _draggedView.hidden = NO;
        //        CGRect startRect = CGRectMake(10, 690, 60, 48);
        CGRect startRect = CGRectMake(45, 720, 1, 1);
        [_draggedView genieOutTransitionWithDuration:0.5
                                           startRect:startRect
                                           startEdge:BCRectEdgeRight
                                          completion:^{
                                              _shoppingCartIsOpened = YES;
                                          }];
        
    } else {
        
        CGRect endRect = CGRectMake(45, 720, 1, 1);
        [_draggedView genieInTransitionWithDuration:0.5
                                    destinationRect:endRect
                                    destinationEdge:BCRectEdgeRight
                                         completion:^{
                                             _shoppingCartIsOpened = NO;
                                         }];
        _draggedView.hidden = YES;
    }
    
    
}



//重写添加子控制器方法
- (void)addChildViewController:(UIViewController *)childController
{
    [super addChildViewController:childController];
    if ([childController isKindOfClass:[HomePageViewController class]]) {
        childController.view.frame = CGRectMake(80, 80, SCREEN_WIDTH-80, SCREEN_HEIGHT -80);
        [self.view addSubview:childController.view];
    }else{
        childController.view.frame = CGRectMake(80, SCREEN_HEIGHT, SCREEN_WIDTH-80, SCREEN_HEIGHT -80);
        [self.view addSubview:childController.view];
    }
    //这句话什么意思不是很懂
    [childController didMoveToParentViewController:self];
}



/**拿到当前推送的view*/
- (UIView *)getCurrentView:(NSInteger)tag{
    switch (tag) {
        case 200:
            return _homePageVC.view;
            break;
        case 201:
            return _serviceVC.view;
            break;
        case 202:
            return _productVC.view;
            break;
        case 203:
            return _listVC.view;
            break;
        case 204:
            return _progressVC.view;
            break;
        default:
            return nil;
            break;
    }

}

/** 返回登录按钮 */
- (void)backToLoginView:(UIButton *)sender
{
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *addDelegate = (AppDelegate *) app.delegate;
    
    [UIView animateWithDuration:0.5 animations:^{
        addDelegate.loginImage.frame = SCREEN_Bounds;
        addDelegate.loginView.frame = SCREEN_Bounds;
        [addDelegate.username becomeFirstResponder];
        addDelegate.loginTextFeild.frame = CGRectMake(320, 90, 395, 500);
    }];
    
}

-(ShoppingCartView *)draggedView
{
    if (!_draggedView) {
        _draggedView = [[ShoppingCartView alloc]initWithFrame:CGRectMake(115, 95, 880, 640)];
        _draggedView.hidden = YES;
        [self.view addSubview:_draggedView];
    }
    
    return _draggedView;
}

- (UIButton *)back
{
    if (_back == nil) {
        _back = [UIButton buttonWithType:UIButtonTypeCustom];
        _back.frame = CGRectMake(65, 5, 40, 40);
        [_back setImage:[UIImage imageNamed:@"关闭_05.png"] forState:UIControlStateNormal];
        _back.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 20);
        [_back addTarget:self action:@selector(backToLoginView:) forControlEvents:UIControlEventTouchUpInside];
        [_ZZNav addSubview:_back];
    }
    return _back;
}

- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 25, 70, 20)];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        [_ZZNav addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)passwordLabel
{
    if (_passwordLabel == nil) {
        _passwordLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 45, 70, 20)];
        _passwordLabel.textAlignment = NSTextAlignmentLeft;
        [_ZZNav addSubview:_passwordLabel];
    }
    return _passwordLabel;
}

#pragma mark 购物车上面显示的按钮

- (UIImageView *)numberImage{
    static CGFloat const ImageWidth = 30;
    static CGFloat const ImageHeight = 30;
    static CGFloat const kNameLabelX = 40;
    static CGFloat const kNameLabelY = -10;

    if (!_numberImage) {
        _numberImage = [[UIImageView alloc] initWithFrame:CGRectMake(kNameLabelX, kNameLabelY, ImageWidth, ImageHeight)];
        _numberImage.image = [UIImage imageNamed:@"o_01"];
    }
    return _numberImage;
}

-(UILabel *)numberLabel
{
    if (!_numberLabel) {
        static CGFloat const LabelWidth = 20;
        static CGFloat const LabelHeight = 20;

        
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.center = _numberImage.center;
        _numberLabel.bounds = CGRectMake(0, 0, LabelWidth, LabelHeight);
        _numberLabel.font = [UIFont systemFontOfSize:12];
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        _numberLabel.text = @"0";
        _numberLabel.textColor = [UIColor whiteColor];
        
    }
    return _numberLabel;
}

@end
